// gscan.cpp : Defines the entry point for the console application.
//

//#include<stdafx.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<ctype.h>
#include<vector>

using namespace std;

 
  /*****************************************************************/
  /*                                                               *
  /*   Module       : To find the best regions in a given sequence *
  /*   Developed by :Saptarshi Sinha							   *
  /*															   *
  /*															   *
  /*****************************************************************/




struct findstsp
 {
	  int st;
	  int sp;
	  int frm;
	  findstsp(int a,int b,int c)
	  {st=a;sp=b;frm=c;}
 };


 struct bestreg
 {
	 int st;
	 int sp;
	 int frm;
	 bestreg(int a,int b,int c)
	 {st=a;sp=b;frm=c;}


 };

 struct bestreg1
 {
	 int st;
	 int sp;
	 int frm;
	 bestreg1(int a,int b,int c)
	 {st=a;sp=b;frm=c;}
 };

   class cdregion
   {
     int Frame,i;
	 int stPos;
	 int spPos;
	 char fpath[50],tempfile[50],tempfilec[50];;
	 
	 char tempstr[4];
	 int genelength;
	 int shift;
	 int stcodpos,spcodpos;
	 
	 FILE *fp1,*fp2,*fp3,*fpc;
	 fpos_t stps;    
	 fpos_t curpos,curposrev;
	 	 
	 char ch,ch1;
	
	 vector <findstsp>fstsp;
	 vector <findstsp>::iterator itstsp;
	 vector <bestreg> brg;
	 vector <bestreg>::iterator itbrg;
	 vector <bestreg1> brg1;
	 vector <bestreg1>::iterator itbrg1;

	 vector <findstsp>fstspc;
	 vector <findstsp>::iterator itstspc;
	 vector <bestreg> brgc;
	 vector <bestreg>::iterator itbrgc;
	 vector <bestreg1> brg1c;
	 vector <bestreg1>::iterator itbrg1c;

    public:
	 
	 void getUserdetails();  
	 void findstspcodon();
	 void findbestreg();
	 void Createnewfile();
	 void display();


	 cdregion()
	 {}
	 
	 ~cdregion()
	 {}


   };//end of class declaration


  
   
   void cdregion::Createnewfile()
   {

	   fpos_t begpos;
       

	   
	   strcpy(tempfile,"codereg.tmp");

	   if((fp2=fopen(tempfile,"wb"))==NULL)
	   {
		   cout<<"Could not create file"<<endl;
		   exit(1);
	   }


       begpos=0;
     
	   fsetpos(fp1,&begpos);

	   while((ch=fgetc(fp1))!=EOF)
	   {
		   if(ch=='\n')
		   {
			   ch=fgetc(fp1);
		   }

		   fputc(ch,fp2);
	   }
	   

   //create a temporary file for complement


	   strcpy(tempfilec,"codereg1.tmp");

	   if((fpc=fopen(tempfilec,"wb"))==NULL)
	   {
		   cout<<"Could not create file"<<endl;
		   exit(1);
	   }


       begpos=0;
     
	   fsetpos(fp1,&begpos);

	   while((ch=fgetc(fp1))!=EOF)
	   {
		   if(ch=='\n')
		   {
			   ch=fgetc(fp1);
		   }

		   if(toupper(ch)=='A')
			   ch1='T';
		   if(toupper(ch)=='T')
     		   ch1='A';
 
		   if(toupper(ch)=='G')
			   ch1='C';
		   
		   if(toupper(ch)=='C')
			   ch1='G';
  		   		   
		   fputc(ch1,fpc);
	   }

   
	   
	   
	   
	   
	   _fcloseall();
   
   
   }


 
  
  void cdregion::getUserdetails()
  {
	   printf("Enter the file name with full path:");
	   gets(fpath);
	   
	   fp1=fopen(fpath,"r");
	   if(fp1==NULL)
	   {
		   cout<<"Could not open file"<<endl;
		   exit(1);
	   }
	   
	   printf("Enter the shift:");
	   cin>>shift;
	   
	   printf("Enter the starting coding region:");
	   cin>>stPos;
	   
	   printf("Enter the ending coding region:");
	   cin>>spPos;
	   
	   printf("Enter the gene length:");
	   cin>>genelength;
 	   
  }




  void cdregion::findstspcodon()
  {	  
	  
	  char tempstr[4];
	  int j;
	      
	  int statg=(stPos-1)-(shift);
	  int spatg=(stPos-1) +(shift) ;
	  
	  int sttaa=(spPos-1)-(shift);
	  int sptaa=(spPos-1)+(shift);
	  
	  int statgr,spatgr,sttaar,sptaar;
	       	  	  
	  if(statg<0)
	   statg=0;

	  fp2=fopen(tempfile,"r");

	  if(fp2==NULL)
	  {
		 cout<<"Path not correct"<<endl;
		 exit(1);

      }
	  Frame=1;

	int statg1=statg;
	int sttaa1=sttaa;
	int atgf=0,taaf=0;

    
   while(Frame<=3)
   {

        //search for atg

	   int readnochar=(spatg-statg1)+1;
	   
	   stps=statg1;
	   fsetpos(fp2,&stps);
	   curpos=stps;
	 
	   for(i=0;i<readnochar;)
	   {
		   for(j=0;j<3;j++,i++)
		   {
			 ch=fgetc(fp2);
			 tempstr[j]=toupper(ch);
			 curpos++;
			
			 if(i==readnochar)
			  {
			 		 j++;	
					 break;
			  }
		 		                
		   }//end for		 
						 
	   
     	   tempstr[j]='\0';
           

		   if(strcmp(tempstr,"ATG")==0)
		   {
			  			   
			  atgf=1;
			  stcodpos=curpos-3; 
			  break;
		   }
	   
	   }//end for


    
       while((sttaa1-statg1)%3 !=0)
	   {
		   sttaa1++;
	   }

     //search for stop codons;

       readnochar=(sptaa-sttaa1)+1;
	   stps=sttaa1;

	   fsetpos(fp2,&stps);
	   	   
	   curpos=stps;
	   
	   for(i=0;i<readnochar;)
	   {
		   for(int j=0;j<3;j++,i++)
		   {

			 ch=fgetc(fp2);
			 tempstr[j]=toupper(ch);
			 curpos++;
			 			 
			 if(i==readnochar)
			 {
	    	   j++;
      		   break;
			 }
			 
		   }

		    tempstr[j]='\0';
		   
		    if(ch==EOF)
			   break;


		   if(strcmp(tempstr,"TAA")==0 || strcmp(tempstr,"TAG")==0 ||strcmp(tempstr,"TGA")==0)
		   {			  
			   
			  taaf=1;
			  spcodpos=curpos-1;
			  break;
		   
		   }//end of if
	   }//end for 

       
	   if(atgf==1 && taaf==1)
	   {

		  //if(Frame==3)
		  fstsp.push_back(findstsp(stcodpos,spcodpos,Frame));
		  atgf=0;
		  taaf=0;
	   }
		 
	  statg1++;
	  sttaa1++;
	  Frame++;
     

   }//end while


	//search in  reverse
	

	fpc=fopen(tempfilec,"r");

	 if(fpc==NULL)
	 {
		 cout<<"Path not correct"<<endl;
		 exit(1);

	 }

	
	statgr=sptaa;
	spatgr=sttaa;
	sttaar=spatg;
	sptaar=statg;
    Frame=1;
    atgf=0;
	taaf=0;



	while(Frame<=3)
	{

        //search for atg

	   int readnochar=(statgr-spatgr)+1;
	   
	   stps=statgr;
	   fsetpos(fpc,&stps);
	   curposrev=stps;
	 
	   for(i=0;i<readnochar;)
	   {
		   for(j=0;j<3;j++,i++)
		   {
			 ch=fgetc(fpc);
			 tempstr[j]=toupper(ch);
			 curposrev--;
			 fsetpos(fpc,&curposrev);
			
			 if(i==readnochar)
			  {
			 		 j++;	
					 break;
			  }
		 		                
		   }//end for		 
						 
	   
     	   tempstr[j]='\0';
           

		   if(strcmp(tempstr,"ATG")==0)
		   {
			  atgf=1;
			  stcodpos=curposrev+3; 
			  break;
		   }
	   
	   }//end for


    
       while((statgr-sttaar)%3 !=0)
	   {
		   sttaar--;
	   }

     //search for stop codons;

       readnochar=(sttaar-sptaar)+1;
	   stps=sttaar;

	   fsetpos(fpc,&stps);
	   	   
	   curposrev=stps;
	   
	   for(i=0;i<readnochar;)
	   {
		   for(int j=0;j<3;j++,i++)
		   {

			 ch=fgetc(fpc);
			 tempstr[j]=toupper(ch);
			 curposrev--;
			 fsetpos(fpc,&curposrev);
			 			 
			 if(i==readnochar)
			 {
	    	   j++;
      		   break;
			 }
			 
		   }

		    tempstr[j]='\0';
		   
		 
		   if(strcmp(tempstr,"TAA")==0 || strcmp(tempstr,"TAG")==0 ||strcmp(tempstr,"TGA")==0)
		   {			  
			   
			  taaf=1;
			  spcodpos=curposrev+1;
			  break;
		   
		   }//end of if
	   }//end for 

       
	   if(atgf==1 && taaf==1)
	   {
		 
		  fstspc.push_back(findstsp(stcodpos,spcodpos,(Frame * -1)));
		  atgf=0;
		  taaf=0;
	   }
		 
	  statgr--;
	  sttaar--;
	  Frame++;
     

   }//end while
  

 
  }//end of main function



 void cdregion::findbestreg()
 {
    
   int fatg=0,ftaa=0;
               
   for(itstsp=fstsp.begin();itstsp!=fstsp.end();itstsp++)
   {
	
      int j;
	  	
      int readnochar=(itstsp->sp - itstsp->st)+1;
	  
	  stps=itstsp->st;
	  fsetpos(fp2,&stps);
	  curpos=stps;
	  i=0;
	   
	  //search for start 
	  while(i<readnochar)
	  {
	 
	    while(true)
		{
		   for(j=0;j<3;j++,i++)
		   {
			 ch=fgetc(fp2);
			 curpos++;
             
			 tempstr[j]=toupper(ch);
			
			 if(i==readnochar)
		      break;
					 
		   }

		   tempstr[j]='\0';

		   		   		   
		   if(strcmp(tempstr,"ATG")==0)
		   {
			      
			  fatg=1;
			  stcodpos=curpos-3;
			  break;
		     
		   }

		  if(i==readnochar)
		    break;
		  
		}//end of while true

		   //search for stops 

		while(true)
		{
		   fsetpos(fp2,&curpos);  
		   
		   for(int j=0;j<3;j++,i++)
		   {
			 ch=fgetc(fp2);
			 curpos++;
			 
			 tempstr[j]=toupper(ch);
			 
			 if(i==readnochar)
			    break;
		   
		   }

		   tempstr[j]='\0';

		   if(strcmp(tempstr,"TAA")==0 || strcmp(tempstr,"TAG")==0 ||strcmp(tempstr,"TGA")==0)
		   {
			   
			   ftaa=1;
			   spcodpos=curpos-1; 
			   break;
			   
		   }

		   if(i==readnochar)
		    break;
  
		}//while true
		  
		if((spcodpos-2)-stcodpos >=genelength && (fatg==1 && ftaa==1) )
		{
 		    					
			brg.push_back(bestreg(stcodpos,spcodpos,itstsp->frm));
		    fatg=0;
			ftaa=0;
		  	  
		}			  


	  }//end of main while
    

  }//end of for



  //search best region for complement base in reverse

  fatg=0;ftaa=0;


  for(itstspc=fstspc.begin();itstspc!=fstspc.end();itstspc++)
  {
	
      int j;
	  
	
      int readnochar=(itstspc->st - itstspc->sp)+1;
	  
	  stps=itstspc->st;
	  fsetpos(fpc,&stps);
	  curposrev=stps;
	  i=0;
	   
	  //search for start 
	  while(i<readnochar)
	  {
	 
	    while(true)
		{
		   for(j=0;j<3;j++,i++)
		   {
			 ch=fgetc(fpc);
			 curposrev--;
             tempstr[j]=toupper(ch);
			 fsetpos(fpc,&curposrev);
			
			 if(i==readnochar)
		      break;
					 
		   }

		   tempstr[j]='\0';

		   		   		   
		   if(strcmp(tempstr,"ATG")==0)
		   {
			  
			  fatg=1;
			  stcodpos=curposrev+3;
			  break;
		     
		   }

		  if(i==readnochar)
		    break;
		  
		}//end of while true

		   //search for stops 

		while(true)
		{
		   fsetpos(fpc,&curposrev);  
		   
		   for(int j=0;j<3;j++,i++)
		   {
			 ch=fgetc(fpc);
			 curposrev--;
			 fsetpos(fpc,&curposrev);
			 
			 tempstr[j]=toupper(ch);
			 
			 if(i==readnochar)
			    break;
		   
		   }

		   tempstr[j]='\0';

		   if(strcmp(tempstr,"TAA")==0 || strcmp(tempstr,"TAG")==0 ||strcmp(tempstr,"TGA")==0)
		   {
			   ftaa=1;
			   spcodpos=curposrev+1; 
			   break;
			   
		   }

		   if(i==readnochar)
		    break;
  
		}//while true
		  
		if(stcodpos-(spcodpos+2) >=genelength && (fatg==1 && ftaa==1) )
		{
 		    					
			brgc.push_back(bestreg(stcodpos,spcodpos,itstspc->frm));
		    fatg=0;
			ftaa=0;
		  	  
		}			  


	  }//end of main while

	      


  }//end of for



 
   
   
   int length,movcur,movcurrev;
   int atgpos;      
  
	//check for atg's in between
  
   for(itbrg=brg.begin();itbrg!=brg.end();itbrg++)
  {

	 	 
	   length=(itbrg->sp-itbrg->st) + 1;

	   movcur=3;
	   stps=itbrg->st +3;
	   fsetpos(fp2,&stps);
 
	   while(movcur<length)
	   {

		  for(int j=0;j<3;j++,movcur++)
		  {
			  ch=fgetc(fp2);
			  tempstr[j]=toupper(ch);

		  }
		  tempstr[j]='\0';

		  if(strcmp(tempstr,"ATG")==0)
		  {
			  
			  fgetpos(fp2,&stps);
			  atgpos=stps-3;
			  
			  if(((itbrg->sp -2)-atgpos) >=genelength)
			  {
			   brg1.push_back(bestreg1(atgpos,itbrg->sp,itbrg->frm));
			   
			  }
			  else
				  break;


		  }

	   }//end of while
	 	 
	 
  }//end for



  //search for in between atg's for complement

  for(itbrgc=brgc.begin();itbrgc!=brgc.end();itbrgc++)
  {

	 	 
	   length=(itbrgc->st-itbrgc->sp) + 1;

	   movcurrev=3;
	   stps=itbrgc->st -3;
	   fsetpos(fpc,&stps);
	   curposrev=stps;
 
	   while(movcur<length)
	   {

		  for(int j=0;j<3;j++,movcur++)
		  {
			  ch=fgetc(fpc);
			  tempstr[j]=toupper(ch);
			  curposrev--;
			  fsetpos(fpc,&curposrev);

		  }
		  tempstr[j]='\0';

		  if(strcmp(tempstr,"ATG")==0)
		  {
			  
			  fgetpos(fpc,&stps);
			  atgpos=stps+3;
			  
			  if((atgpos-(itbrgc->sp+2)) >=genelength)
			  {
			   brg1c.push_back(bestreg1(atgpos,itbrgc->sp,itbrgc->frm));
			   
			  }
			  else
				  break;


		  }

	   }//end of while
	 
	 
	 
  }//end for


 }//end of main func


 
 void cdregion::display()
 {
	 
	int pos;
	//char dfile[50];
    
	//printf("Enter the destination file name:");
	//gets(dfile);


   fp3=fopen("d:\\saptarshi\\brg1.txt","w");
    
   if(fp3==NULL)
   {
		 cout<<"Could not create destination File"<<endl;
		 exit(1);
   }


  fprintf(fp3,"\n%s %d %s %d %s %d %s %d\n\n","Coding region:",stPos,"To",spPos,"Genelength>=",genelength,"Shift:",shift);
  
  
  /*for(itstsp=fstsp.begin();itstsp!=fstsp.end();itstsp++)
  {
	  fprintf(fp3,"%d %d %d\n",itstsp->st+1,itstsp->sp+1,itstsp->frm);
  }
  
  
  for(itstspc=fstspc.begin();itstspc!=fstspc.end();itstspc++)
  {
	  fprintf(fp3,"%d %d %d\n",itstspc->st+1,itstspc->sp+1,itstspc->frm);
  }*/
  
  
   
  
  for(itbrg=brg.begin();itbrg!=brg.end();itbrg++)
  {
    				
	   int totread= (itbrg->sp -itbrg->st) + 1;
      
	   stps=itbrg->st;
	   fsetpos(fp2,&stps);
	   pos=0;
		
	   fprintf(fp3,"%d %d %d %d\n\n",itbrg->st+1 ,itbrg->sp+1,itbrg->frm,(itbrg->sp -itbrg->st-2));
		
	   while(pos<totread)
	   {

		 ch=fgetc(fp2);
     	 fputc(ch,fp3);
		 pos++;
	 
	   } 
	   
	   fprintf(fp3,"\n\n");
  
  }//end of for


  for(itbrg1=brg1.begin();itbrg1!=brg1.end();itbrg1++)
  {
    	    
	
	   int totread= (itbrg1->sp -itbrg1->st) + 1;
      
	    stps=itbrg1->st;
		fsetpos(fp2,&stps);
		pos=0;
		
		fprintf(fp3,"%d %d %d %d\n\n",itbrg1->st+1 ,itbrg1->sp+1,itbrg1->frm,((itbrg1->sp -2) -itbrg1->st));
		
		while(pos<totread)
		{

		  ch=fgetc(fp2);
     	  fputc(ch,fp3);
		  pos++;
			 
		} 
		fprintf(fp3,"\n\n");

  
  }//end of for



  for(itbrgc=brgc.begin();itbrgc!=brgc.end();itbrgc++)
  {
    	
				
	   int totread= (itbrgc->st -itbrgc->sp) + 1;
      
	   stps=itbrgc->sp;
	   fsetpos(fpc,&stps);
	   pos=0;
		
	   fprintf(fp3,"%d %d %d %d\n\n",itbrgc->st+1 ,itbrgc->sp+1,itbrgc->frm,(itbrgc->st -(itbrgc->sp+2)));
		
	   while(pos<totread)
	   {

		 ch=fgetc(fpc);
     	 fputc(ch,fp3);
		 pos++;
	 
	   } 
	   
	   fprintf(fp3,"\n\n");
	
  
  }//end of for



  for(itbrg1c=brg1c.begin();itbrg1c!=brg1c.end();itbrg1c++)
  {
    	    
	
	    int totread= (itbrg1c->st -itbrg1c->sp) + 1;
      
	    stps=itbrg1c->sp;
	    fsetpos(fpc,&stps);
		pos=0;
		
		fprintf(fp3,"%d %d %d %d\n\n",itbrg1c->st+1 ,itbrg1c->sp+1,itbrg1c->frm,((itbrg1c->st- (itbrg1c->sp+2))));
		
		while(pos<totread)
		{
		  ch=fgetc(fpc);
     	  fputc(ch,fp3);
		  pos++;
			 
		} 
		fprintf(fp3,"\n\n");
		 
	  
  }//end of for


  _fcloseall(); //close all open files

   //char delfile[50];
     
   remove(tempfile);
   remove(tempfilec);
   
  
 }//end func



  void main()
  {
		
	cdregion Cr;
	
	Cr.getUserdetails();
	Cr.Createnewfile();
	Cr.findstspcodon();
	Cr.findbestreg();
	Cr.display();

	 	
   }
